﻿package com.eshore.player;

import android.util.Log;


/**
 * 取流+解码
 * 
 * @author heshaoguo
 * 
 */
public class PlayerMainThread extends Thread {

	ImagePlayerViewRtmp view = null;
	protected boolean isConnected = false;
	protected int returnvalue = 0;

	public PlayerMainThread(ImagePlayerViewRtmp view) {
		this.view = view;
		this.isConnected = false;
	}

	@Override
	public synchronized void run() {
		Log.d("PlayerMainThread", "PlayerMainThread start");
		this.isConnected = false;
		this.returnvalue = 0;
		try {
			this.returnvalue = ImagePlayerView.ffplayStop();
		} catch (Exception e) {
			this.returnvalue = 1;
		}
		try {
			sleep(10);
		} catch (InterruptedException e) {
			// IshowEvList.d("PlayerMainThread", "ffplay cancel connect onstop.");
			this.Stop();
			return;
		}
		try {
			if (this.view == null) {
				// IshowEvList.d("PlayerMainThread", "ffplay isnot view, cancel connect.");
				this.Stop();
				return;
			} else {
				Log.d("PlayerMainThread", "ffplay open connect "
							+ this.view.getRtmpUrl());
				this.returnvalue = ImagePlayerView.ffplayConnect(this.view
						.getRtmpUrl());
			}
		} catch (Exception e) {
			this.returnvalue = 1;
		}
		try {
			sleep(10);
		} catch (InterruptedException e) {
			// IshowEvList.d("PlayerMainThread", "ffplay cancel connect.");
			this.pause();
			return;
		}
		if (this.returnvalue != 0) {
			// IshowEvList.d("PlayerMainThread", "ffplay havenot connect.");
			this.pause();
			return;
		}
		// 准备显示
		this.isConnected = true;
		try {
			if (this.view == null) {
				this.pause();
				return;
			} else {
				this.view.startPlayerIsReadyThread();
			}
		} catch (Exception e) {
			this.pause();
			return;
		}
		try {
			this.returnvalue = ImagePlayerView.ffplayPlay();
		} catch (Exception e) {
			this.returnvalue = 1;
		}
		if (this.returnvalue != 0) {
			// IshowEvList.d("PlayerMainThread", "ffplay cannot play");
			this.pause();
			return;
		}
	}
	
	public void Stop(){
		this.view = null;
		if (this.isConnected) {
			try {
				ImagePlayerView.ffplayStop();
				Log.d("PlayerMainThread", "ffplay stop");
			} catch (Exception e) {

			}
		}
		this.isConnected = false;
	}
	public void resume1(){
		if (this.isConnected) {
			try {
				ImagePlayerView.ffplayResume();
				Log.d("PlayerMainThread", "ffplay resume");
			} catch (Exception e) {

			}
		}
	}
	public void pause() {
		
		if (this.isConnected) {
			try {
				ImagePlayerView.ffplayPause();
				Log.d("PlayerMainThread", "ffplay pause");
			} catch (Exception e) {

			}
		}
	}

	@Override
	public synchronized void start() {
		super.start();
	}

}
